#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory then create some           
#   new files, do an Incremental and restore those two files.
# test the bvfs interface
#
TestName="bvfs-restore-test"
JobName=Incremental 
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs
echo "${tmpsrc}" >${tmp}/file-list
echo "$tmp/hl" >>${tmp}/file-list

mkdir -p $tmpsrc $tmp/hl
cp -p $src/src/dird/*.c $tmpsrc
cd $tmpsrc
for i in *.c
do
    for j in `seq 1 100`; do
        ln $i $tmp/hl/$i.hl.$j
    done
done
cd $cwd

change_jobname CompressedTest $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
label storage=File volume=TestVolume001
run job=$JobName yes
wait
@sleep 1
@exec "touch $tmpsrc/dird.c"
run job=$JobName yes
wait
@sleep 1
@exec "touch $tmpsrc/ua_cmds.c"
run job=$JobName yes
wait
@sleep 1
@exec "touch $tmpsrc/ua_dotcmds.c"
run job=$JobName yes
wait
messages
.bvfs_update
quit
END_OF_DATA

run_bacula

cat <<EOF >$tmp/bconcmds
@$out /dev/null
@$out $tmp/log0.out
.bvfs_lsdirs jobid=1,2,3,4 path=
@$out $tmp/log3.out
.bvfs_lsfiles jobid=1,2,3,4 path=$tmp/hl/ pattern=verify.c
@$out $tmp/log4.out
.bvfs_lsdirs path=$tmp/ jobid=1 pattern=hl
@$out $tmp/log5.out
sql
select FileIndex,Filename from File WHERE JobId=1 and FileIndex > 0 and FileIndex < 4;

quit
EOF

run_bconsole

# The hl/ directory ID can change, we take it from a bvfs_lsdir
hldirid=`awk '/hl\/$/ { print $1 }' $tmp/log4.out`
fid=`awk "/^$hldirid/"' { m=(m>$2)?m:$2 } END { print m }' $tmp/log3.out`

dirid=`awk '/\/$/ { print $1 }' $tmp/log0.out`

cat <<EOF >$tmp/bconcmds
@$out $tmp/log2.out
setdebug level=15 tags=bvfs,sql trace=1 dir
.bvfs_cleanup path=b21$$
.bvfs_restore path=b21$$ fileid= dirid=$dirid hardlink= jobid=1,2,3,4
restore file=?b21$$ where=$tmp/bacula-restore yes
wait
.bvfs_cleanup path=b211$$
.bvfs_restore path=b211$$ fileid=$fid dirid= hardlink= jobid=1,2,3,4
restore file=?b211$$ where=$tmp/bacula-restore2 yes
wait
messages
.bvfs_cleanup path=b211$$
.bvfs_restore path=b211$$  jobid=1,2 fileindex=1,1,1,2,1,3
restore file=?b211$$ where=$tmp/bacula-restore3 yes
wait
messages
@$out $tmp/log22.out
@# Will not work due to the filter
.bvfs_cleanup path=b211$$
.bvfs_restore path=b211$$  jobid=3 fileindex=1,1,1,2,1,3
messages
quit
EOF

run_bconsole

stop_bacula
touch $tmp/log2.out
check_two_logs

$rscripts/diff.pl -s $tmp/bacula-restore/$tmpsrc -d $tmpsrc
if [ $? != 0 ]; then
    print_debug "ERROR: found difference after restore"
    estat=1
fi

nb=`find $tmp/bacula-restore2/ -type f | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: found difference after restore of a hardlink"
    estat=1
fi

mode=`awk -F= '/Inserting hardlinks method=/ { print $2 ; exit}' $cwd/working/*trace`
print_debug "INFO: Was using $mode method"

nb=`find $tmp/bacula-restore3/ -type f | wc -l`
if [ $nb != 3 ]; then
    print_debug "ERROR: found difference after fileindex restore"
    estat=1
fi

grep "Can't create restore list" $tmp/log22.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: found difference after fileindex restore 2"
    estat=1
fi

end_test
